const { Blog, User} = require('../utils/db')

const writeBlog = (username, avatar, title, content) => {
    const publish_date = Date.now()
    const blog = new Blog({
        username,
        avatar,
        title,
        content,
        publish_date,
    })
    return blog.save()
}

const findOne = (id) => {
    return Blog.findOne({'_id': id})
}

const find = () => {
    return Blog.find()
}

const writeComment = async (author, article, content, reply) => {
    const blog = await findOne(article)
    const created = Date.now()
    blog.comment.push({
        author,
        reply,
        content,
        created,
    })
    return blog.save()
}

module.exports = {
    writeBlog,
    findOne,
    find,
    writeComment,
}